package pagerank;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class FLQuickSort_Rec  {
	protected static int compareOneToTwo(FLURLPageRankStruct x, FLURLPageRankStruct y)
	{
		if (x.PageRankValue < y.PageRankValue)
		{
			return -1;
		}
		else if(x.PageRankValue > y.PageRankValue)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	protected static int getPivot(ArrayList<FLURLPageRankStruct> des, int indexLeft, int indexRight)
	{
		return indexLeft;
	}
	protected static void qsort(ArrayList<FLURLPageRankStruct> des, int indexLeft, int indexRight)
	{
		if (indexLeft >= indexRight)
		{
			return;
		}
		if (indexLeft == indexRight - 1)
		{
			int resultC = compareOneToTwo(des.get(indexLeft), des.get(indexRight));
			if (resultC == 1)
			{
				FLURLPageRankStruct temp = des.get(indexLeft);
				des.set(indexLeft, des.get(indexRight));
				des.set(indexRight, temp);
			}
			return;
		}
		int firstPivot = getPivot(des,indexLeft,indexRight);
		int i = indexLeft,j = indexRight;
		{
			des.set(firstPivot, des.get(indexLeft));
		}
		FLURLPageRankStruct pivotValue = des.get(firstPivot);
		int emptyIndex = firstPivot;
		while(true)
		{
			if ( i == j)
			{
				des.set(j, pivotValue);
				break;
			}
			while(true)
			{
				if ( i == j)
				{
					des.set(j, pivotValue);
					break;
				}
				if (compareOneToTwo(pivotValue , des.get(j)) == 1)
				{
					des.set(emptyIndex,des.get(j));
					emptyIndex = j;
					break;
				}
				j --;
			}
			while(true)
			{
				if ( i == j)
				{
					des.set(i, pivotValue);
					break;
				}
				if (compareOneToTwo(pivotValue , des.get(i)) == -1)
				{
					des.set(emptyIndex,des.get(i));
					emptyIndex = i;
					break;
				}
				i ++;
			}
		}
		qsort(des,indexLeft, i);
		if (i < indexRight + 1)
		{
			qsort(des,i + 1, indexRight);
		}
	}
	public static void main(String args[])
	{
		ArrayList<FLURLPageRankStruct> a = new ArrayList<FLURLPageRankStruct>();
//		a.add(new FLURLPageRankStruct("fl0.3",0.3));
//		a.add(new FLURLPageRankStruct("fl0.4",0.4));
//		a.add(new FLURLPageRankStruct("fl0.1",0.1));
//		a.add(new FLURLPageRankStruct("fl0.001",0.001));
//		a.add(new FLURLPageRankStruct("fl0.01",0.01));
//		a.add(new FLURLPageRankStruct("fl0.01",0.01));
		
		File dataFile = new File("123PageRankResult.txt");
//		File dataFile = new File("123.txt");
		FileReader fr;
		try {
			fr = new FileReader(dataFile);
			BufferedReader br = new BufferedReader(fr);
			String oneString;
			oneString = br.readLine();
			int count = 0;
			while (oneString != null)
			{
				String thisURL,thisPA;
				String[] as = oneString.split(",");
				thisURL = as[1];
				thisPA = as[2];
				FLURLPageRankStruct thisStruct = new FLURLPageRankStruct(thisURL, Double.parseDouble(thisPA));
				a.add(thisStruct);
				oneString = br.readLine();
				count++;
				if (count > 5000)
					break;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		qsort(a,0,a.size()-1);
		for (int i=0; i<a.size(); i++)
		{
			System.out.println(a.get(i));
		}
	}
}
